﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExplorerPlus.PersistenceLayer.Xml
{
    public class Repository
    {
        private string name;

        /// <summary>
        /// Returns the catalog metada xml file path.
        /// </summary>
        public string MetadataFilePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\data\\metadata\\" + this.name + ".xml";
            }
        }

        /// <summary>
        /// Returns the catalog data xml file path.
        /// </summary>
        public string DataFilePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\data\\" + this.name + ".xml";
            }
        }

        /// <summary>
        /// Initializes a repository for a certain catalog.
        /// </summary>
        /// <param name="repositoryName">Catalog name</param>
        public Repository(string repositoryName)
        {
            this.name = repositoryName;
        }

        /// <summary>
        /// Loads a catalog metadata data set from the metadata xml file.
        /// </summary>
        public Metadata LoadMetadata()
        {
            if (System.IO.File.Exists(this.MetadataFilePath))
            {
                Metadata meta = new Metadata();
                meta.ReadXml(this.MetadataFilePath);
                return meta;
            }
            else
            {
                return new Metadata();
            }
        }

        /// <summary>
        /// Updates a catalog metadata dataset and stores its information to xml file.
        /// </summary>
        public void UpdateMetadata(Metadata meta)
        {
            if (meta.HasChanges())
            {
                meta.WriteXml(this.MetadataFilePath);
                meta.AcceptChanges();
            }
        }

        /// <summary>
        /// Loads a catalog datastore data set from the data xml file.
        /// </summary>
        public DataStore LoadDataStore()
        {
            if (System.IO.File.Exists(this.DataFilePath))
            {
                DataStore ds = new DataStore();
                ds.ReadXml(this.DataFilePath);
                ds.AcceptChanges();
                return ds;
            }
            else
            {
                return new DataStore();
            }
        }

        /// <summary>
        /// Updates a catalog datastore dataset and stores its information to xml file.
        /// </summary>
        public bool UpdateDataStore(DataStore ds)
        {
            if (ds.HasChanges())
            {
                ds.WriteXml(this.DataFilePath);
                ds.AcceptChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// This method is used to switch between different catalogs.
        /// </summary>
        public void SwitchRepository(string repositoryName)
        {
            this.name = repositoryName;
        }
    }
}
